
/**
 *	TrueMusic - simple java mp3 player
 *
 *	TrueMusic is free software; you can redistribute it
 *	and/or modify it under the terms of the GNU General
 *	Public License as published by the Free Software
 *	Foundation; either version 2 of the License or (at
 *	your option) any later version.
 *
 *	This program is distributed in the hope that it will be
 *	useful but WITHOUT ANY WARRANTY; without even the
 *	implied warranty of the MERCHANTABILITY or FITNESS FOR
 *	A PARTICULAR PURPOSE. See the GNU General Public
 *	License for more details.
 *
 *	Copyright (c)2005-2007 Ryan Macnak <rmacnak@gmail.com>
 */

package com.servequake.truemusic;

import java.io.*;
import javax.sound.sampled.*;

import org.xiph.speex.spi.*;

/**
 * Ogg Vorbis audio implemented by Jorbis decoder.
 */
public class JSpeexAudio implements Audio, Runnable
{
	String title="";
	String artist="";
	String album="";
	String track="";
	String lyrics="";

	String res;
	PlaybackListener l;

	SourceDataLine line;
	AudioInputStream ais;
	
	public void validate(PlaybackListener l)
	{
		this.l = l;
	}
  
	public void load(String res, PlaybackListener l) throws Exception
	{
		this.l = l;
		this.res = res;
	}
	
	public synchronized void play()
	{
		try
		{
			ais = new SpeexAudioFileReader().getAudioInputStream(new FileInputStream(res));
			
			line = getOutputLine( ais.getFormat().getChannels(), ais.getFormat().getSampleRate() );
			
			int size = ais.getFormat().getFrameSize();
			size = 512;
			
			while(true)
			{
				byte[] b = new byte[size];
				
				int read = ais.read(b,0,size);
				line.write(b, 0, read);
			}
			
			
		
		}catch(Exception e){e.printStackTrace();}
	}
	
	public synchronized void pause(){}
	public synchronized void stop(){}
	
	public synchronized void seek(int frame){}
	public int getFrames(){return 0;}

	public void run(){}
	
	SourceDataLine getOutputLine(int channels, float rate)
	{
		try
		{
			AudioFormat af = new AudioFormat( rate, 16, channels, true, false );
			DataLine.Info info = new DataLine.Info(SourceDataLine.class, af, AudioSystem.NOT_SPECIFIED);

			SourceDataLine outputLine = (SourceDataLine) AudioSystem.getLine(info);
			outputLine.open(af);
			outputLine.start();
			
			//vol = (FloatControl)outputLine.getControl(FloatControl.Type.MASTER_GAIN);
			return outputLine;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean isPlaying(){return false;}

	public String getResource(){return res;}
	public String getTitle(){if(title=="") return new File(res).getName(); return title;}
	public String getArtist(){return artist;}
	public String getAlbum(){return album;}
	public String getTrack(){return track;}
	public String getLyrics(){return lyrics;}
	public String getGenre(){return "";}
	public String toString()
	{if(title=="") return new File(res).getName(); return "\""+title+"\" by "+artist;}

	public String formatName(){return "Speex (doesn't work yet)";}
	public String[] getExtensions(){return new String[]{".spx"};}	
}
